package lg.minecraft.plugin.pokedex.config;

import com.cobblemon.mod.common.pokemon.Species;
import lg.minecraft.plugin.cobblemoncore.util.PokemonDataCache;
import lg.minecraft.plugin.library.config.LGBaseConfig;
import lg.minecraft.plugin.pokedex.LGPokedex;
import lombok.Getter;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
@Getter
public class RewardConfig extends LGBaseConfig {

    public static final RewardConfig INSTANCE = new RewardConfig();

    private GenEntry gen1;
    private GenEntry gen2;
    private GenEntry gen3;
    private GenEntry gen4;
    private GenEntry gen5;
    private GenEntry gen6;
    private GenEntry gen7;
    private GenEntry gen8;
    private GenEntry gen9;
    private GenEntry allGen;

    private List<PokemonEntry> pokemonRewardList;

    public RewardConfig() {
        super(LGPokedex.getInstance(), "reward");
        init();
    }

    @Override
    public void load() {
        super.load();

        this.gen1 = new GenEntry(getStringList("gen1.addLore"), getStringList("gen1.commands"));
        this.gen2 = new GenEntry(getStringList("gen2.addLore"), getStringList("gen2.commands"));
        this.gen3 = new GenEntry(getStringList("gen3.addLore"), getStringList("gen3.commands"));
        this.gen4 = new GenEntry(getStringList("gen4.addLore"), getStringList("gen4.commands"));
        this.gen5 = new GenEntry(getStringList("gen5.addLore"), getStringList("gen5.commands"));
        this.gen6 = new GenEntry(getStringList("gen6.addLore"), getStringList("gen6.commands"));
        this.gen7 = new GenEntry(getStringList("gen7.addLore"), getStringList("gen7.commands"));
        this.gen8 = new GenEntry(getStringList("gen8.addLore"), getStringList("gen8.commands"));
        this.gen9 = new GenEntry(getStringList("gen9.addLore"), getStringList("gen9.commands"));
        this.allGen = new GenEntry(getStringList("all_gen.addLore"), getStringList("all_gen.commands"));

        pokemonRewardList = new ArrayList<>();
        ConfigurationSection pokemonSection = getConfigurationSection("pokemon");
        if (pokemonSection != null) {
            for (String key : pokemonSection.getKeys(false)) {
                String name = pokemonSection.getString(key + ".species");
                Species species = null;
                if (name != null) {
                    species = PokemonDataCache.getInstance().getSpeciesByKey(name);
                    if (species == null) {
                        LGPokedex.getInstance().getLogger().warning("Failed to get species " + name + " for " + key);
                    }
                }
                List<String> labels = pokemonSection.getStringList(key + ".labels");
                List<String> addLore = pokemonSection.getStringList(key + ".addLore");
                List<String> commands = pokemonSection.getStringList(key + ".commands");
                pokemonRewardList.add(new PokemonEntry(species, labels, addLore, commands));
            }
        }
    }

    public PokemonEntry getPokemonReward(@NotNull Species species) {
        for (PokemonEntry entry : pokemonRewardList) {
            if (entry.matches(species)) {
                return entry;
            }
        }
        return null;
    }

    public record GenEntry(List<String> addLore, List<String> commands) {}

    public record PokemonEntry(Species species, List<String> labels, List<String> addLore, List<String> commands) {
        public boolean matches(@NotNull Species species) {
            if (this.species != null) {
                return this.species == species;
            }
            HashSet<String> labels = species.getLabels();
            for (String label : this.labels) {
                if (!labels.contains(label)) {
                    return false;
                }
            }
            return true;
        }
    }

}
